package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.di.uav.applet.mapper.SpecialPricingMapper;
import com.xin.di.uav.applet.po.SpecialPricing;
import com.xin.di.uav.applet.service.ISpecialPricingService;
import com.xin.di.uav.common.request.pricing.SpecialPricingReq;
import com.xin.di.uav.common.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 定价表 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2024-11-13
 */
@Service
public class SpecialPricingServiceImpl extends ServiceImpl<SpecialPricingMapper, SpecialPricing> implements ISpecialPricingService {

    @Autowired
    private SpecialPricingMapper specialPricingMapper;

    @Override
    public SpecialPricing getPrice(String takeoffCode, String landingCode, String pricingId) {
        QueryWrapper<SpecialPricing> specialPricingQueryWrapper = new QueryWrapper<>();
        specialPricingQueryWrapper.lambda().eq(SpecialPricing::getTakeoffCode, takeoffCode)
                .eq(SpecialPricing::getLandingCode, landingCode)
                .eq(SpecialPricing::getPricingId, pricingId);
        return specialPricingMapper.selectOne(specialPricingQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addBatchSpecialPricing(List<SpecialPricingReq> specialPricingReqList) {
        List<SpecialPricing> specialPricings = BeanUtil.copyProperties(specialPricingReqList, SpecialPricing.class);
        return this.saveBatch(specialPricings);
    }

    @Override
    public Boolean saveOrUpdateSpBatch(String pricingId, List<String> spIds, List<SpecialPricingReq> spReqList) {
        //删除本次更新不在继续保留的历史特殊定价
        this.remove(new LambdaQueryWrapper<SpecialPricing>().eq(SpecialPricing::getPricingId, pricingId).notIn(CollUtil.isNotEmpty(spIds), SpecialPricing::getId, spIds));
        List<SpecialPricing> sps = BeanUtil.copyProperties(spReqList, SpecialPricing.class);
        return this.saveOrUpdateBatch(sps);
    }

}
